package com.wushi.lenovo.asia5b.app_ui.profile.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mvp.base.BaseMvpActivity;
import com.mvp.base.model.ResultModel;
import com.mvp.common.GlobalValue;
import com.mvp.ui.view.BaseTextWatcher;
import com.mvp.utils.ToastUtil;
import com.mvp.utils.https.MapHelper;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_di.APPComponentFactory;
import com.wushi.lenovo.asia5b.app_di.other.presenter.CodePresenter;
import com.wushi.lenovo.asia5b.app_di.profile.mvpview.ChangeLoginPassActivityMvpView;
import com.wushi.lenovo.asia5b.app_di.profile.presenter.ChangeLoginPassActivityPresenter;
import com.wushi.lenovo.asia5b.app_event.ChangeProfileEvent;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_model.profile.CodeModel;
import com.wushi.lenovo.asia5b.app_model.profile.bean.ChangeProfileType;
import com.wushi.lenovo.asia5b.app_utils.WToastHelper;
import com.wushi.lenovo.asia5b.app_utils.eye.EyeUtils;
import com.wushi.lenovo.asia5b.app_widget.ErrorTextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import timber.log.Timber;

/**
 * Created by zhangyuncai on 2018/12/6.
 * 修改登录密码
 */
public class ChangeLoginPassActivity extends BaseMvpActivity<
        ChangeLoginPassActivityMvpView, ChangeLoginPassActivityPresenter> implements ChangeLoginPassActivityMvpView {
    @BindView(R.id.et_changepass_oldpass)
    EditText et_changepass_oldpass;
    @BindView(R.id.et_changepass_newpass)
    EditText et_changepass_newpass;
    @BindView(R.id.et_changepass_newpass2)
    EditText et_changepass_newpass2;
    @BindView(R.id.bt_changepass_save)
    Button bt_changepass_save;
    @BindView(R.id.bt_changepass_cancle)
    Button bt_changepass_cancle;
    @BindView(R.id.et_changepass_code)
    EditText et_changepass_code;
    @BindView(R.id.tv_changepass_getcode)
    TextView tv_changepass_getcode;
    @BindView(R.id.tv_changepass_newpass2_error)
    ErrorTextView tv_changepass_newpass2_error;
    @BindView(R.id.tv_changepass_code_error)
    ErrorTextView tv_changepass_code_error;
    @BindView(R.id.tv_changepass_oldpass_error)
    ErrorTextView tv_changepass_oldpass_error;
    @BindView(R.id.iv_changepass_oldpass_eye)
    ImageView iv_changepass_oldpass_eye;
    @BindView(R.id.iv_changepass_newpass_eye)
    ImageView iv_changepass_newpass_eye;
    @BindView(R.id.iv_changepass_newpass2_eye)
    ImageView iv_changepass_newpass2_eye;
    @BindView(R.id.ll_changepass_haspass)
    LinearLayout ll_changepass_haspass;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_changeloginpass;
    }

    @Override
    protected void constructComponent() {
        APPComponentFactory.getAppBaseComponent().inject(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {
        ll_changepass_haspass.setVisibility(UserInfo.getUserInfo().getHas_login_pass() ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        EyeUtils.bindEye(iv_changepass_oldpass_eye, et_changepass_oldpass);
        EyeUtils.bindEye(iv_changepass_newpass_eye, et_changepass_newpass);
        EyeUtils.bindEye(iv_changepass_newpass2_eye, et_changepass_newpass2);
        et_changepass_newpass2.addTextChangedListener(new BaseTextWatcher() {
            @Override
            public void onTextChanged(CharSequence charSequence, int start, int count, int after) {
                String pass = et_changepass_newpass.getText().toString().trim();
                tv_changepass_newpass2_error.setVisibility(TextUtils.equals(pass, charSequence) ? View.GONE : View.VISIBLE);
            }
        });
        tv_changepass_code_error.setBindEditText(et_changepass_code);
        tv_changepass_newpass2_error.setBindEditText(et_changepass_newpass2, et_changepass_newpass);
        tv_changepass_oldpass_error.setBindEditText(et_changepass_oldpass);
    }

    private void save() {
        String oldPass = et_changepass_oldpass.getText().toString().trim();
        String newPass = et_changepass_newpass.getText().toString().trim();
        String newPass2 = et_changepass_newpass2.getText().toString().trim();
        String code = et_changepass_code.getText().toString().trim();

        boolean b = false;
        if (!TextUtils.equals(newPass, newPass2)) {
            tv_changepass_newpass2_error.setVisibility(View.VISIBLE);
            b = true;
        }

        if (UserInfo.getUserInfo().getHas_login_pass() && TextUtils.isEmpty(oldPass)) {
            tv_changepass_oldpass_error.setVisibility(View.VISIBLE);
            b = true;
        }
        if (TextUtils.isEmpty(newPass)) {
            tv_changepass_newpass2_error.setVisibility(View.VISIBLE);
            b = true;
        }

        if (TextUtils.isEmpty(code)) {
            tv_changepass_code_error.setText(R.string.filed_is_empty);
            tv_changepass_code_error.setVisibility(View.VISIBLE);
            b = true;
        } else {
            Object tag = et_changepass_code.getTag();
            if (tag != null && tag instanceof String) {
                String tagCode = (String) tag;
                if (!TextUtils.equals(tagCode, code)) {
                    tv_changepass_code_error.setText(R.string.invalid_code);
                    tv_changepass_code_error.setVisibility(View.VISIBLE);
                    b = true;
                }
            }
        }
        if (b) {
            return;
        }
        Map<String, Object> map = MapHelper.newHashMap();
        map.put("member_id", "" + UserInfo.getMemberId());
        if (UserInfo.getUserInfo().getHas_login_pass()) {
            map.put("old_login_password", "" + oldPass);
        }
        map.put("new_login_password", "" + newPass);
        map.put("otp", "" + code);
        getPresenter().changePass(map);


    }

    private void cancel() {
        finish();
    }

    @OnClick({
            R.id.bt_changepass_save,
            R.id.bt_changepass_cancle,
            R.id.tv_changepass_getcode,
            R.id.tv_changepass_forget
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_changepass_save:
                save();
                break;
            case R.id.bt_changepass_cancle:
                cancel();
                break;
            case R.id.tv_changepass_getcode:
                et_changepass_code.setTag("");
                getPresenter().sendCode();
                break;
            case R.id.tv_changepass_forget:
                startActivity(new Intent(this, CheckEmailActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void changePassResult(ResultModel resultModel) {
        if (resultModel.isSuccess()) {
            WToastHelper.showContentView(this, R.string.changeloginpass_success);
            UserInfo.getUserInfo().setHas_login_pass("1");
            finish();
        } else {
            String msg = resultModel.getMsg();
            if (!TextUtils.isEmpty(msg)) {
                if (msg.contains("password invalid")) {
                    tv_changepass_oldpass_error.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public void sendCodeResult(ResultModel<CodeModel> resultModel, CodePresenter.CodeType codeType) {
        if (resultModel != null && resultModel.isSuccess()) {
            ToastUtil.showToast(getString(R.string.otp_send_success));
            getPresenter().interval();
            CodeModel codeModel = resultModel.getData();
            if (codeModel != null) {
                et_changepass_code.setTag(codeModel.getOTP_code());
                if (GlobalValue.isDebug) {
                    et_changepass_code.setText(codeModel.getOTP_code());
                }
            }
        }

    }

    @Override
    public void intervalResult(long aLong) {
        Timber.d("--->>>>>>>>---aLong:" + aLong);
        if (aLong == 0) {
            tv_changepass_getcode.setEnabled(true);
            tv_changepass_getcode.setText(R.string.chongxinhuoquyanzhengma);
        } else {
            tv_changepass_getcode.setEnabled(false);
            tv_changepass_getcode.setText(aLong + "s");
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onChangeProfileEvent(ChangeProfileEvent event) {
        if (event != null && event.changeProfileType == ChangeProfileType.ChangeLoginPass) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
